// pages/modifyPSW/modifyPSW.js
const app = getApp()
const util = require('../../utils/util.js')

Page({
  data: {},

  onLoad: function(options) {
    console.log(options)
    if (options.type == 1) {
      this.setData({
        type: '姓名',
        data: app.globalData.realname
      })
    } else {
      this.setData({
        type: '学号',
        data: app.globalData.phone
      })
    }
  },


  formSubmit: util.throttle(function(e) {
    let that = this,
      oldData = this.data.data,
      data = app.trimAll(e.detail.value.data)
    if (!data || data == '') {
      wx.showModal({
        content: "请输入要修改的信息",
        showCancel: false,
      })
    } else if (data == oldData) {
      wx.showModal({
        content: "与原信息相同，无需修改！",
        showCancel: false,
      })
    } else {
      wx.showModal({
        title: '注意',
        content: "您所提交的个人信息，将关系到您的签到记录，请务必确保个人信息的真实性，谨慎修改！",
        success: res => {
          if (res.confirm) {
            let reqData = {
              weChatId: app.globalData.wechatId,
              name: null,
              account: null
            }
            if ('姓名' == that.data.type) {
              reqData.name = data
            } else {
              reqData.account = data
            }
            wx.request({
              url: app.baseUrl + '/WeChat/AttendanceSys/updateUser',
              method: 'POST',
              data: reqData,
              dataType: 'text',
              success: res => {
                console.log(res)
                if ('success' == res.data) {
                  wx.showToast({
                    title: '修改成功！',
                    mask: true,
                    success: () => {
                      reqData.name == null ? app.globalData.phone = reqData.account : app.globalData.realname = reqData.name, app.globalData.username = reqData.name
                      // console.log(app.globalData)
                      setTimeout(function() {
                        wx.navigateBack({
                          delta: 1
                        })
                      }, 1500)
                    }
                  })
                } else {
                  wx.showModal({
                    content: "服务器异常，请稍后再试！",
                    showCancel: false,
                  })
                }
              },
              fail: res => {
                wx.showToast({
                  title: '网络异常！',
                  image: '/images/warning.png'
                })
              }


            })
          }
        }
      })
    }
  })
})